package com.devbobcorn.nekoration.client.rendering.entities;

import java.util.List;

import com.devbobcorn.nekoration.entities.WallPaperEntity;
import com.devbobcorn.nekoration.entities.WallPaperEntity.Part;
import com.google.common.collect.Lists;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.datafixers.util.Pair;

import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.EntityRendererManager;
import net.minecraft.client.renderer.model.ModelBakery;
import net.minecraft.client.renderer.model.ModelRenderer;
import net.minecraft.client.renderer.tileentity.BannerTileEntityRenderer;
import net.minecraft.item.DyeColor;
import net.minecraft.tileentity.BannerPattern;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.vector.Vector3f;

public class WallPaperRenderer extends EntityRenderer<WallPaperEntity> {
    public static final ModelRenderer paperFull = makePaper(Part.FULL);
	public static final ModelRenderer paperUpper = makePaper(Part.UPPER);
	public static final ModelRenderer paperLower = makePaper(Part.LOWER);

	public WallPaperRenderer(EntityRendererManager manager) {
		super(manager);
	}

    private static ModelRenderer makePaper(Part part) {
        ModelRenderer paper = new ModelRenderer(64, 64, 0, (part == Part.LOWER) ? 20 : 0);
		switch (part){
			case FULL:
				paper.addBox(-10.0F, 0.0F, 0.0F, 20.0F, 40.0F, 1.0F, 0.0F);
				paper.y = -20.0F;
				break;
			default:
				paper.addBox(-10.0F, 0.0F, 0.0F, 20.0F, 20.0F, 1.0F, 0.0F);
				paper.y = -10.0F;
				break;
		}
        paper.z = -0.5F;
        return paper;
    }

	public static ModelRenderer getPaper(Part part) {
		switch (part){
			case FULL:
				return paperFull;
			case LOWER:
				return paperLower;
			default:
				return paperUpper;
		}
	}

	public void render(WallPaperEntity entity, float rotation, float partialTicks, MatrixStack stack, IRenderTypeBuffer buffers, int packedLight) {
		stack.pushPose();
		stack.mulPose(Vector3f.YP.rotationDegrees(180.0F - rotation));

		renderWallPaper(stack, buffers, entity, entity.getHeight(), packedLight);
		stack.popPose();
		super.render(entity, rotation, partialTicks, stack, buffers, packedLight);
	}

	@Override
	public ResourceLocation getTextureLocation(WallPaperEntity entity) {
		return null;
	}

    public static List<Pair<BannerPattern, DyeColor>> getBlankPattern(DyeColor base){
        List<Pair<BannerPattern, DyeColor>> list = Lists.newArrayList();
        list.add(Pair.of(BannerPattern.BASE, base));
        return list;
    }

	private void renderWallPaper(MatrixStack stack, IRenderTypeBuffer buffers, WallPaperEntity entity, int height, int light) {
        // Then render the wallpaper
		float sc = 0.8F;
		stack.scale(sc, sc, sc);
		stack.mulPose(Vector3f.ZP.rotationDegrees(180.0F));

		BannerTileEntityRenderer.renderPatterns(stack, buffers, light, 0xFFFFFF, getPaper(entity.getPart()), ModelBakery.BANNER_BASE, true, (entity.getPatterns() == null) ? getBlankPattern(entity.getBaseColor()) : entity.getPatterns());
	}
}
